{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 程序说明\n",
    "时间：2016年11月22日\n",
    "\n",
    "说明：finetune InceptionV3网络。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.加载keras模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "from keras.applications.inception_v3 import InceptionV3\n",
    "from keras.preprocessing import image\n",
    "from keras.models import Model\n",
    "from keras.layers import Dense, GlobalAveragePooling2D\n",
    "from keras import backend as K"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 如需绘制模型请加载plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from keras.utils.visualize_util import plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.2/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5\n",
      "86695936/86916664 [============================>.] - ETA: 0s"
     ]
    }
   ],
   "source": [
    "# create the base pre-trained model\n",
    "base_model = InceptionV3(weights='imagenet', include_top=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "____________________________________________________________________________________________________\n",
      "Layer (type)                     Output Shape          Param #     Connected to                     \n",
      "====================================================================================================\n",
      "input_1 (InputLayer)             (None, None, None, 3) 0                                            \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_1 (Convolution2D)  (None, None, None, 32)896         input_1[0][0]                    \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_1 (BatchNormal(None, None, None, 32)64          convolution2d_1[0][0]            \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_2 (Convolution2D)  (None, None, None, 32)9248        batchnormalization_1[0][0]       \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_2 (BatchNormal(None, None, None, 32)64          convolution2d_2[0][0]            \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_3 (Convolution2D)  (None, None, None, 64)18496       batchnormalization_2[0][0]       \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_3 (BatchNormal(None, None, None, 64)128         convolution2d_3[0][0]            \n",
      "____________________________________________________________________________________________________\n",
      "maxpooling2d_1 (MaxPooling2D)    (None, None, None, 64)0           batchnormalization_3[0][0]       \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_4 (Convolution2D)  (None, None, None, 80)5200        maxpooling2d_1[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_4 (BatchNormal(None, None, None, 80)160         convolution2d_4[0][0]            \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_5 (Convolution2D)  (None, None, None, 192138432      batchnormalization_4[0][0]       \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_5 (BatchNormal(None, None, None, 192384         convolution2d_5[0][0]            \n",
      "____________________________________________________________________________________________________\n",
      "maxpooling2d_2 (MaxPooling2D)    (None, None, None, 1920           batchnormalization_5[0][0]       \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_9 (Convolution2D)  (None, None, None, 64)12352       maxpooling2d_2[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_9 (BatchNormal(None, None, None, 64)128         convolution2d_9[0][0]            \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_7 (Convolution2D)  (None, None, None, 48)9264        maxpooling2d_2[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_10 (Convolution2D) (None, None, None, 96)55392       batchnormalization_9[0][0]       \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_7 (BatchNormal(None, None, None, 48)96          convolution2d_7[0][0]            \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_10 (BatchNorma(None, None, None, 96)192         convolution2d_10[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "averagepooling2d_1 (AveragePoolin(None, None, None, 1920           maxpooling2d_2[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_6 (Convolution2D)  (None, None, None, 64)12352       maxpooling2d_2[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_8 (Convolution2D)  (None, None, None, 64)76864       batchnormalization_7[0][0]       \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_11 (Convolution2D) (None, None, None, 96)83040       batchnormalization_10[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_12 (Convolution2D) (None, None, None, 32)6176        averagepooling2d_1[0][0]         \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_6 (BatchNormal(None, None, None, 64)128         convolution2d_6[0][0]            \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_8 (BatchNormal(None, None, None, 64)128         convolution2d_8[0][0]            \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_11 (BatchNorma(None, None, None, 96)192         convolution2d_11[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_12 (BatchNorma(None, None, None, 32)64          convolution2d_12[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "mixed0 (Merge)                   (None, None, None, 2560           batchnormalization_6[0][0]       \n",
      "                                                                   batchnormalization_8[0][0]       \n",
      "                                                                   batchnormalization_11[0][0]      \n",
      "                                                                   batchnormalization_12[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_16 (Convolution2D) (None, None, None, 64)16448       mixed0[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_16 (BatchNorma(None, None, None, 64)128         convolution2d_16[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_14 (Convolution2D) (None, None, None, 48)12336       mixed0[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_17 (Convolution2D) (None, None, None, 96)55392       batchnormalization_16[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_14 (BatchNorma(None, None, None, 48)96          convolution2d_14[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_17 (BatchNorma(None, None, None, 96)192         convolution2d_17[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "averagepooling2d_2 (AveragePoolin(None, None, None, 2560           mixed0[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_13 (Convolution2D) (None, None, None, 64)16448       mixed0[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_15 (Convolution2D) (None, None, None, 64)76864       batchnormalization_14[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_18 (Convolution2D) (None, None, None, 96)83040       batchnormalization_17[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_19 (Convolution2D) (None, None, None, 32)8224        averagepooling2d_2[0][0]         \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_13 (BatchNorma(None, None, None, 64)128         convolution2d_13[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_15 (BatchNorma(None, None, None, 64)128         convolution2d_15[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_18 (BatchNorma(None, None, None, 96)192         convolution2d_18[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_19 (BatchNorma(None, None, None, 32)64          convolution2d_19[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "mixed1 (Merge)                   (None, None, None, 2560           batchnormalization_13[0][0]      \n",
      "                                                                   batchnormalization_15[0][0]      \n",
      "                                                                   batchnormalization_18[0][0]      \n",
      "                                                                   batchnormalization_19[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_23 (Convolution2D) (None, None, None, 64)16448       mixed1[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_23 (BatchNorma(None, None, None, 64)128         convolution2d_23[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_21 (Convolution2D) (None, None, None, 48)12336       mixed1[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_24 (Convolution2D) (None, None, None, 96)55392       batchnormalization_23[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_21 (BatchNorma(None, None, None, 48)96          convolution2d_21[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_24 (BatchNorma(None, None, None, 96)192         convolution2d_24[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "averagepooling2d_3 (AveragePoolin(None, None, None, 2560           mixed1[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_20 (Convolution2D) (None, None, None, 64)16448       mixed1[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_22 (Convolution2D) (None, None, None, 64)76864       batchnormalization_21[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_25 (Convolution2D) (None, None, None, 96)83040       batchnormalization_24[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_26 (Convolution2D) (None, None, None, 32)8224        averagepooling2d_3[0][0]         \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_20 (BatchNorma(None, None, None, 64)128         convolution2d_20[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_22 (BatchNorma(None, None, None, 64)128         convolution2d_22[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_25 (BatchNorma(None, None, None, 96)192         convolution2d_25[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_26 (BatchNorma(None, None, None, 32)64          convolution2d_26[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "mixed2 (Merge)                   (None, None, None, 2560           batchnormalization_20[0][0]      \n",
      "                                                                   batchnormalization_22[0][0]      \n",
      "                                                                   batchnormalization_25[0][0]      \n",
      "                                                                   batchnormalization_26[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_28 (Convolution2D) (None, None, None, 64)16448       mixed2[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_28 (BatchNorma(None, None, None, 64)128         convolution2d_28[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_29 (Convolution2D) (None, None, None, 96)55392       batchnormalization_28[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_29 (BatchNorma(None, None, None, 96)192         convolution2d_29[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_27 (Convolution2D) (None, None, None, 384885120      mixed2[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_30 (Convolution2D) (None, None, None, 96)83040       batchnormalization_29[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_27 (BatchNorma(None, None, None, 384768         convolution2d_27[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_30 (BatchNorma(None, None, None, 96)192         convolution2d_30[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "maxpooling2d_3 (MaxPooling2D)    (None, None, None, 2560           mixed2[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "mixed3 (Merge)                   (None, None, None, 7360           batchnormalization_27[0][0]      \n",
      "                                                                   batchnormalization_30[0][0]      \n",
      "                                                                   maxpooling2d_3[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_35 (Convolution2D) (None, None, None, 12894336       mixed3[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_35 (BatchNorma(None, None, None, 128256         convolution2d_35[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_36 (Convolution2D) (None, None, None, 128114816      batchnormalization_35[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_36 (BatchNorma(None, None, None, 128256         convolution2d_36[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_32 (Convolution2D) (None, None, None, 12894336       mixed3[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_37 (Convolution2D) (None, None, None, 128114816      batchnormalization_36[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_32 (BatchNorma(None, None, None, 128256         convolution2d_32[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_37 (BatchNorma(None, None, None, 128256         convolution2d_37[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_33 (Convolution2D) (None, None, None, 128114816      batchnormalization_32[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_38 (Convolution2D) (None, None, None, 128114816      batchnormalization_37[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_33 (BatchNorma(None, None, None, 128256         convolution2d_33[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_38 (BatchNorma(None, None, None, 128256         convolution2d_38[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "averagepooling2d_4 (AveragePoolin(None, None, None, 7360           mixed3[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_31 (Convolution2D) (None, None, None, 192141504      mixed3[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_34 (Convolution2D) (None, None, None, 192172224      batchnormalization_33[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_39 (Convolution2D) (None, None, None, 192172224      batchnormalization_38[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_40 (Convolution2D) (None, None, None, 192141504      averagepooling2d_4[0][0]         \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_31 (BatchNorma(None, None, None, 192384         convolution2d_31[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_34 (BatchNorma(None, None, None, 192384         convolution2d_34[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_39 (BatchNorma(None, None, None, 192384         convolution2d_39[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_40 (BatchNorma(None, None, None, 192384         convolution2d_40[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "mixed4 (Merge)                   (None, None, None, 7680           batchnormalization_31[0][0]      \n",
      "                                                                   batchnormalization_34[0][0]      \n",
      "                                                                   batchnormalization_39[0][0]      \n",
      "                                                                   batchnormalization_40[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_45 (Convolution2D) (None, None, None, 160123040      mixed4[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_45 (BatchNorma(None, None, None, 160320         convolution2d_45[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_46 (Convolution2D) (None, None, None, 160179360      batchnormalization_45[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_46 (BatchNorma(None, None, None, 160320         convolution2d_46[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_42 (Convolution2D) (None, None, None, 160123040      mixed4[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_47 (Convolution2D) (None, None, None, 160179360      batchnormalization_46[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_42 (BatchNorma(None, None, None, 160320         convolution2d_42[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_47 (BatchNorma(None, None, None, 160320         convolution2d_47[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_43 (Convolution2D) (None, None, None, 160179360      batchnormalization_42[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_48 (Convolution2D) (None, None, None, 160179360      batchnormalization_47[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_43 (BatchNorma(None, None, None, 160320         convolution2d_43[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_48 (BatchNorma(None, None, None, 160320         convolution2d_48[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "averagepooling2d_5 (AveragePoolin(None, None, None, 7680           mixed4[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_41 (Convolution2D) (None, None, None, 192147648      mixed4[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_44 (Convolution2D) (None, None, None, 192215232      batchnormalization_43[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_49 (Convolution2D) (None, None, None, 192215232      batchnormalization_48[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_50 (Convolution2D) (None, None, None, 192147648      averagepooling2d_5[0][0]         \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_41 (BatchNorma(None, None, None, 192384         convolution2d_41[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_44 (BatchNorma(None, None, None, 192384         convolution2d_44[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_49 (BatchNorma(None, None, None, 192384         convolution2d_49[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_50 (BatchNorma(None, None, None, 192384         convolution2d_50[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "mixed5 (Merge)                   (None, None, None, 7680           batchnormalization_41[0][0]      \n",
      "                                                                   batchnormalization_44[0][0]      \n",
      "                                                                   batchnormalization_49[0][0]      \n",
      "                                                                   batchnormalization_50[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_55 (Convolution2D) (None, None, None, 160123040      mixed5[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_55 (BatchNorma(None, None, None, 160320         convolution2d_55[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_56 (Convolution2D) (None, None, None, 160179360      batchnormalization_55[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_56 (BatchNorma(None, None, None, 160320         convolution2d_56[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_52 (Convolution2D) (None, None, None, 160123040      mixed5[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_57 (Convolution2D) (None, None, None, 160179360      batchnormalization_56[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_52 (BatchNorma(None, None, None, 160320         convolution2d_52[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_57 (BatchNorma(None, None, None, 160320         convolution2d_57[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_53 (Convolution2D) (None, None, None, 160179360      batchnormalization_52[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_58 (Convolution2D) (None, None, None, 160179360      batchnormalization_57[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_53 (BatchNorma(None, None, None, 160320         convolution2d_53[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_58 (BatchNorma(None, None, None, 160320         convolution2d_58[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "averagepooling2d_6 (AveragePoolin(None, None, None, 7680           mixed5[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_51 (Convolution2D) (None, None, None, 192147648      mixed5[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_54 (Convolution2D) (None, None, None, 192215232      batchnormalization_53[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_59 (Convolution2D) (None, None, None, 192215232      batchnormalization_58[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_60 (Convolution2D) (None, None, None, 192147648      averagepooling2d_6[0][0]         \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_51 (BatchNorma(None, None, None, 192384         convolution2d_51[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_54 (BatchNorma(None, None, None, 192384         convolution2d_54[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_59 (BatchNorma(None, None, None, 192384         convolution2d_59[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_60 (BatchNorma(None, None, None, 192384         convolution2d_60[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "mixed6 (Merge)                   (None, None, None, 7680           batchnormalization_51[0][0]      \n",
      "                                                                   batchnormalization_54[0][0]      \n",
      "                                                                   batchnormalization_59[0][0]      \n",
      "                                                                   batchnormalization_60[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_65 (Convolution2D) (None, None, None, 160123040      mixed6[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_65 (BatchNorma(None, None, None, 160320         convolution2d_65[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_66 (Convolution2D) (None, None, None, 192215232      batchnormalization_65[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_66 (BatchNorma(None, None, None, 192384         convolution2d_66[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_62 (Convolution2D) (None, None, None, 192147648      mixed6[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_67 (Convolution2D) (None, None, None, 192258240      batchnormalization_66[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_62 (BatchNorma(None, None, None, 192384         convolution2d_62[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_67 (BatchNorma(None, None, None, 192384         convolution2d_67[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_63 (Convolution2D) (None, None, None, 192258240      batchnormalization_62[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_68 (Convolution2D) (None, None, None, 192258240      batchnormalization_67[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_63 (BatchNorma(None, None, None, 192384         convolution2d_63[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_68 (BatchNorma(None, None, None, 192384         convolution2d_68[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "averagepooling2d_7 (AveragePoolin(None, None, None, 7680           mixed6[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_61 (Convolution2D) (None, None, None, 192147648      mixed6[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_64 (Convolution2D) (None, None, None, 192258240      batchnormalization_63[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_69 (Convolution2D) (None, None, None, 192258240      batchnormalization_68[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_70 (Convolution2D) (None, None, None, 192147648      averagepooling2d_7[0][0]         \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_61 (BatchNorma(None, None, None, 192384         convolution2d_61[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_64 (BatchNorma(None, None, None, 192384         convolution2d_64[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_69 (BatchNorma(None, None, None, 192384         convolution2d_69[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_70 (BatchNorma(None, None, None, 192384         convolution2d_70[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "mixed7 (Merge)                   (None, None, None, 7680           batchnormalization_61[0][0]      \n",
      "                                                                   batchnormalization_64[0][0]      \n",
      "                                                                   batchnormalization_69[0][0]      \n",
      "                                                                   batchnormalization_70[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_73 (Convolution2D) (None, None, None, 192147648      mixed7[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_73 (BatchNorma(None, None, None, 192384         convolution2d_73[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_74 (Convolution2D) (None, None, None, 192258240      batchnormalization_73[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_74 (BatchNorma(None, None, None, 192384         convolution2d_74[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_71 (Convolution2D) (None, None, None, 192147648      mixed7[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_75 (Convolution2D) (None, None, None, 192258240      batchnormalization_74[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_71 (BatchNorma(None, None, None, 192384         convolution2d_71[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_75 (BatchNorma(None, None, None, 192384         convolution2d_75[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_72 (Convolution2D) (None, None, None, 320553280      batchnormalization_71[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_76 (Convolution2D) (None, None, None, 192331968      batchnormalization_75[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_72 (BatchNorma(None, None, None, 320640         convolution2d_72[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_76 (BatchNorma(None, None, None, 192384         convolution2d_76[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "averagepooling2d_8 (AveragePoolin(None, None, None, 7680           mixed7[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "mixed8 (Merge)                   (None, None, None, 1280           batchnormalization_72[0][0]      \n",
      "                                                                   batchnormalization_76[0][0]      \n",
      "                                                                   averagepooling2d_8[0][0]         \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_81 (Convolution2D) (None, None, None, 448573888      mixed8[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_81 (BatchNorma(None, None, None, 448896         convolution2d_81[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_78 (Convolution2D) (None, None, None, 384491904      mixed8[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_82 (Convolution2D) (None, None, None, 3841548672     batchnormalization_81[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_78 (BatchNorma(None, None, None, 384768         convolution2d_78[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_82 (BatchNorma(None, None, None, 384768         convolution2d_82[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_79 (Convolution2D) (None, None, None, 384442752      batchnormalization_78[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_80 (Convolution2D) (None, None, None, 384442752      batchnormalization_78[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_83 (Convolution2D) (None, None, None, 384442752      batchnormalization_82[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_84 (Convolution2D) (None, None, None, 384442752      batchnormalization_82[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "averagepooling2d_9 (AveragePoolin(None, None, None, 1280           mixed8[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_77 (Convolution2D) (None, None, None, 320409920      mixed8[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_79 (BatchNorma(None, None, None, 384768         convolution2d_79[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_80 (BatchNorma(None, None, None, 384768         convolution2d_80[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_83 (BatchNorma(None, None, None, 384768         convolution2d_83[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_84 (BatchNorma(None, None, None, 384768         convolution2d_84[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_85 (Convolution2D) (None, None, None, 192245952      averagepooling2d_9[0][0]         \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_77 (BatchNorma(None, None, None, 320640         convolution2d_77[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "mixed9_0 (Merge)                 (None, None, None, 7680           batchnormalization_79[0][0]      \n",
      "                                                                   batchnormalization_80[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "merge_1 (Merge)                  (None, None, None, 7680           batchnormalization_83[0][0]      \n",
      "                                                                   batchnormalization_84[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_85 (BatchNorma(None, None, None, 192384         convolution2d_85[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "mixed9 (Merge)                   (None, None, None, 2040           batchnormalization_77[0][0]      \n",
      "                                                                   mixed9_0[0][0]                   \n",
      "                                                                   merge_1[0][0]                    \n",
      "                                                                   batchnormalization_85[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_90 (Convolution2D) (None, None, None, 448917952      mixed9[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_90 (BatchNorma(None, None, None, 448896         convolution2d_90[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_87 (Convolution2D) (None, None, None, 384786816      mixed9[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_91 (Convolution2D) (None, None, None, 3841548672     batchnormalization_90[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_87 (BatchNorma(None, None, None, 384768         convolution2d_87[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_91 (BatchNorma(None, None, None, 384768         convolution2d_91[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_88 (Convolution2D) (None, None, None, 384442752      batchnormalization_87[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_89 (Convolution2D) (None, None, None, 384442752      batchnormalization_87[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_92 (Convolution2D) (None, None, None, 384442752      batchnormalization_91[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_93 (Convolution2D) (None, None, None, 384442752      batchnormalization_91[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "averagepooling2d_10 (AveragePooli(None, None, None, 2040           mixed9[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_86 (Convolution2D) (None, None, None, 320655680      mixed9[0][0]                     \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_88 (BatchNorma(None, None, None, 384768         convolution2d_88[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_89 (BatchNorma(None, None, None, 384768         convolution2d_89[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_92 (BatchNorma(None, None, None, 384768         convolution2d_92[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_93 (BatchNorma(None, None, None, 384768         convolution2d_93[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "convolution2d_94 (Convolution2D) (None, None, None, 192393408      averagepooling2d_10[0][0]        \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_86 (BatchNorma(None, None, None, 320640         convolution2d_86[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "mixed9_1 (Merge)                 (None, None, None, 7680           batchnormalization_88[0][0]      \n",
      "                                                                   batchnormalization_89[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "merge_2 (Merge)                  (None, None, None, 7680           batchnormalization_92[0][0]      \n",
      "                                                                   batchnormalization_93[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "batchnormalization_94 (BatchNorma(None, None, None, 192384         convolution2d_94[0][0]           \n",
      "____________________________________________________________________________________________________\n",
      "mixed10 (Merge)                  (None, None, None, 2040           batchnormalization_86[0][0]      \n",
      "                                                                   mixed9_1[0][0]                   \n",
      "                                                                   merge_2[0][0]                    \n",
      "                                                                   batchnormalization_94[0][0]      \n",
      "====================================================================================================\n",
      "Total params: 21577728\n",
      "____________________________________________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "base_model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf\n"
     ]
    }
   ],
   "source": [
    "# add a global spatial average pooling layer\n",
    "x = base_model.output\n",
    "x = GlobalAveragePooling2D()(x)\n",
    "# let's add a fully-connected layer\n",
    "x = Dense(1024, activation='relu')(x)\n",
    "# and a logistic layer -- let's say we have 200 classes\n",
    "predictions = Dense(200, activation='softmax')(x)\n",
    "\n",
    "# this is the model we will train\n",
    "model = Model(input=base_model.input, output=predictions)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# first: train only the top layers (which were randomly initialized)\n",
    "# i.e. freeze all convolutional InceptionV3 layers\n",
    "for layer in base_model.layers:\n",
    "    layer.trainable = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# compile the model (should be done *after* setting layers to non-trainable)\n",
    "model.compile(optimizer='rmsprop', loss='categorical_crossentropy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-11-af00eea935de>, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-11-af00eea935de>\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m    model.fit_generator(...)\u001b[0m\n\u001b[1;37m                        ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# train the model on the new data for a few epochs\n",
    "model.fit_generator(...)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 'input_1')\n",
      "(1, 'convolution2d_1')\n",
      "(2, 'batchnormalization_1')\n",
      "(3, 'convolution2d_2')\n",
      "(4, 'batchnormalization_2')\n",
      "(5, 'convolution2d_3')\n",
      "(6, 'batchnormalization_3')\n",
      "(7, 'maxpooling2d_1')\n",
      "(8, 'convolution2d_4')\n",
      "(9, 'batchnormalization_4')\n",
      "(10, 'convolution2d_5')\n",
      "(11, 'batchnormalization_5')\n",
      "(12, 'maxpooling2d_2')\n",
      "(13, 'convolution2d_9')\n",
      "(14, 'batchnormalization_9')\n",
      "(15, 'convolution2d_7')\n",
      "(16, 'convolution2d_10')\n",
      "(17, 'batchnormalization_7')\n",
      "(18, 'batchnormalization_10')\n",
      "(19, 'averagepooling2d_1')\n",
      "(20, 'convolution2d_6')\n",
      "(21, 'convolution2d_8')\n",
      "(22, 'convolution2d_11')\n",
      "(23, 'convolution2d_12')\n",
      "(24, 'batchnormalization_6')\n",
      "(25, 'batchnormalization_8')\n",
      "(26, 'batchnormalization_11')\n",
      "(27, 'batchnormalization_12')\n",
      "(28, 'mixed0')\n",
      "(29, 'convolution2d_16')\n",
      "(30, 'batchnormalization_16')\n",
      "(31, 'convolution2d_14')\n",
      "(32, 'convolution2d_17')\n",
      "(33, 'batchnormalization_14')\n",
      "(34, 'batchnormalization_17')\n",
      "(35, 'averagepooling2d_2')\n",
      "(36, 'convolution2d_13')\n",
      "(37, 'convolution2d_15')\n",
      "(38, 'convolution2d_18')\n",
      "(39, 'convolution2d_19')\n",
      "(40, 'batchnormalization_13')\n",
      "(41, 'batchnormalization_15')\n",
      "(42, 'batchnormalization_18')\n",
      "(43, 'batchnormalization_19')\n",
      "(44, 'mixed1')\n",
      "(45, 'convolution2d_23')\n",
      "(46, 'batchnormalization_23')\n",
      "(47, 'convolution2d_21')\n",
      "(48, 'convolution2d_24')\n",
      "(49, 'batchnormalization_21')\n",
      "(50, 'batchnormalization_24')\n",
      "(51, 'averagepooling2d_3')\n",
      "(52, 'convolution2d_20')\n",
      "(53, 'convolution2d_22')\n",
      "(54, 'convolution2d_25')\n",
      "(55, 'convolution2d_26')\n",
      "(56, 'batchnormalization_20')\n",
      "(57, 'batchnormalization_22')\n",
      "(58, 'batchnormalization_25')\n",
      "(59, 'batchnormalization_26')\n",
      "(60, 'mixed2')\n",
      "(61, 'convolution2d_28')\n",
      "(62, 'batchnormalization_28')\n",
      "(63, 'convolution2d_29')\n",
      "(64, 'batchnormalization_29')\n",
      "(65, 'convolution2d_27')\n",
      "(66, 'convolution2d_30')\n",
      "(67, 'batchnormalization_27')\n",
      "(68, 'batchnormalization_30')\n",
      "(69, 'maxpooling2d_3')\n",
      "(70, 'mixed3')\n",
      "(71, 'convolution2d_35')\n",
      "(72, 'batchnormalization_35')\n",
      "(73, 'convolution2d_36')\n",
      "(74, 'batchnormalization_36')\n",
      "(75, 'convolution2d_32')\n",
      "(76, 'convolution2d_37')\n",
      "(77, 'batchnormalization_32')\n",
      "(78, 'batchnormalization_37')\n",
      "(79, 'convolution2d_33')\n",
      "(80, 'convolution2d_38')\n",
      "(81, 'batchnormalization_33')\n",
      "(82, 'batchnormalization_38')\n",
      "(83, 'averagepooling2d_4')\n",
      "(84, 'convolution2d_31')\n",
      "(85, 'convolution2d_34')\n",
      "(86, 'convolution2d_39')\n",
      "(87, 'convolution2d_40')\n",
      "(88, 'batchnormalization_31')\n",
      "(89, 'batchnormalization_34')\n",
      "(90, 'batchnormalization_39')\n",
      "(91, 'batchnormalization_40')\n",
      "(92, 'mixed4')\n",
      "(93, 'convolution2d_45')\n",
      "(94, 'batchnormalization_45')\n",
      "(95, 'convolution2d_46')\n",
      "(96, 'batchnormalization_46')\n",
      "(97, 'convolution2d_42')\n",
      "(98, 'convolution2d_47')\n",
      "(99, 'batchnormalization_42')\n",
      "(100, 'batchnormalization_47')\n",
      "(101, 'convolution2d_43')\n",
      "(102, 'convolution2d_48')\n",
      "(103, 'batchnormalization_43')\n",
      "(104, 'batchnormalization_48')\n",
      "(105, 'averagepooling2d_5')\n",
      "(106, 'convolution2d_41')\n",
      "(107, 'convolution2d_44')\n",
      "(108, 'convolution2d_49')\n",
      "(109, 'convolution2d_50')\n",
      "(110, 'batchnormalization_41')\n",
      "(111, 'batchnormalization_44')\n",
      "(112, 'batchnormalization_49')\n",
      "(113, 'batchnormalization_50')\n",
      "(114, 'mixed5')\n",
      "(115, 'convolution2d_55')\n",
      "(116, 'batchnormalization_55')\n",
      "(117, 'convolution2d_56')\n",
      "(118, 'batchnormalization_56')\n",
      "(119, 'convolution2d_52')\n",
      "(120, 'convolution2d_57')\n",
      "(121, 'batchnormalization_52')\n",
      "(122, 'batchnormalization_57')\n",
      "(123, 'convolution2d_53')\n",
      "(124, 'convolution2d_58')\n",
      "(125, 'batchnormalization_53')\n",
      "(126, 'batchnormalization_58')\n",
      "(127, 'averagepooling2d_6')\n",
      "(128, 'convolution2d_51')\n",
      "(129, 'convolution2d_54')\n",
      "(130, 'convolution2d_59')\n",
      "(131, 'convolution2d_60')\n",
      "(132, 'batchnormalization_51')\n",
      "(133, 'batchnormalization_54')\n",
      "(134, 'batchnormalization_59')\n",
      "(135, 'batchnormalization_60')\n",
      "(136, 'mixed6')\n",
      "(137, 'convolution2d_65')\n",
      "(138, 'batchnormalization_65')\n",
      "(139, 'convolution2d_66')\n",
      "(140, 'batchnormalization_66')\n",
      "(141, 'convolution2d_62')\n",
      "(142, 'convolution2d_67')\n",
      "(143, 'batchnormalization_62')\n",
      "(144, 'batchnormalization_67')\n",
      "(145, 'convolution2d_63')\n",
      "(146, 'convolution2d_68')\n",
      "(147, 'batchnormalization_63')\n",
      "(148, 'batchnormalization_68')\n",
      "(149, 'averagepooling2d_7')\n",
      "(150, 'convolution2d_61')\n",
      "(151, 'convolution2d_64')\n",
      "(152, 'convolution2d_69')\n",
      "(153, 'convolution2d_70')\n",
      "(154, 'batchnormalization_61')\n",
      "(155, 'batchnormalization_64')\n",
      "(156, 'batchnormalization_69')\n",
      "(157, 'batchnormalization_70')\n",
      "(158, 'mixed7')\n",
      "(159, 'convolution2d_73')\n",
      "(160, 'batchnormalization_73')\n",
      "(161, 'convolution2d_74')\n",
      "(162, 'batchnormalization_74')\n",
      "(163, 'convolution2d_71')\n",
      "(164, 'convolution2d_75')\n",
      "(165, 'batchnormalization_71')\n",
      "(166, 'batchnormalization_75')\n",
      "(167, 'convolution2d_72')\n",
      "(168, 'convolution2d_76')\n",
      "(169, 'batchnormalization_72')\n",
      "(170, 'batchnormalization_76')\n",
      "(171, 'averagepooling2d_8')\n",
      "(172, 'mixed8')\n",
      "(173, 'convolution2d_81')\n",
      "(174, 'batchnormalization_81')\n",
      "(175, 'convolution2d_78')\n",
      "(176, 'convolution2d_82')\n",
      "(177, 'batchnormalization_78')\n",
      "(178, 'batchnormalization_82')\n",
      "(179, 'convolution2d_79')\n",
      "(180, 'convolution2d_80')\n",
      "(181, 'convolution2d_83')\n",
      "(182, 'convolution2d_84')\n",
      "(183, 'averagepooling2d_9')\n",
      "(184, 'convolution2d_77')\n",
      "(185, 'batchnormalization_79')\n",
      "(186, 'batchnormalization_80')\n",
      "(187, 'batchnormalization_83')\n",
      "(188, 'batchnormalization_84')\n",
      "(189, 'convolution2d_85')\n",
      "(190, 'batchnormalization_77')\n",
      "(191, 'mixed9_0')\n",
      "(192, 'merge_1')\n",
      "(193, 'batchnormalization_85')\n",
      "(194, 'mixed9')\n",
      "(195, 'convolution2d_90')\n",
      "(196, 'batchnormalization_90')\n",
      "(197, 'convolution2d_87')\n",
      "(198, 'convolution2d_91')\n",
      "(199, 'batchnormalization_87')\n",
      "(200, 'batchnormalization_91')\n",
      "(201, 'convolution2d_88')\n",
      "(202, 'convolution2d_89')\n",
      "(203, 'convolution2d_92')\n",
      "(204, 'convolution2d_93')\n",
      "(205, 'averagepooling2d_10')\n",
      "(206, 'convolution2d_86')\n",
      "(207, 'batchnormalization_88')\n",
      "(208, 'batchnormalization_89')\n",
      "(209, 'batchnormalization_92')\n",
      "(210, 'batchnormalization_93')\n",
      "(211, 'convolution2d_94')\n",
      "(212, 'batchnormalization_86')\n",
      "(213, 'mixed9_1')\n",
      "(214, 'merge_2')\n",
      "(215, 'batchnormalization_94')\n",
      "(216, 'mixed10')\n"
     ]
    }
   ],
   "source": [
    "# at this point, the top layers are well trained and we can start fine-tuning\n",
    "# convolutional layers from inception V3. We will freeze the bottom N layers\n",
    "# and train the remaining top layers.\n",
    "\n",
    "# let's visualize layer names and layer indices to see how many layers\n",
    "# we should freeze:\n",
    "for i, layer in enumerate(base_model.layers):\n",
    "   print(i, layer.name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# we chose to train the top 2 inception blocks, i.e. we will freeze\n",
    "# the first 172 layers and unfreeze the rest:\n",
    "for layer in model.layers[:172]:\n",
    "   layer.trainable = False\n",
    "for layer in model.layers[172:]:\n",
    "   layer.trainable = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# we need to recompile the model for these modifications to take effect\n",
    "# we use SGD with a low learning rate\n",
    "from keras.optimizers import SGD\n",
    "model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# we train our model again (this time fine-tuning the top 2 inception blocks\n",
    "# alongside the top Dense layers\n",
    "model.fit_generator(...)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  },
  "ssap_exp_config": {
   "error_alert": "Error Occurs!",
   "initial": [],
   "max_iteration": 1000,
   "recv_id": "",
   "running": [],
   "summary": [],
   "version": "1.1.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
